package com.classload.controller;


import com.classload.domain.Product;
import com.classload.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RefreshScope
public class ProductController {
    @Autowired
    ProductService productService;

//    nacos 动态刷新
    @Autowired
    ConfigurableApplicationContext applicationContext;

    @RequestMapping("/product/{pid}")
    Product findByPid(@PathVariable("pid") Integer pid) {
        return productService.findByPid(pid);
    }

    @RequestMapping("test1")
    public String testConfig1(){
        //nacos 硬编码 获取动态刷新属性
        return applicationContext.getEnvironment().getProperty("app.config.name");

    }

    @RequestMapping("testOrDev")
    public void testOrDev(String env){
        //nacos 硬编码 获取动态刷新属性
         applicationContext.getEnvironment().setDefaultProfiles(env);
    }

    // 注解方式动态刷新
    @Value("${app.config.name}")
    private String appConfigName;
    @RequestMapping("test2")
    public String testConfig2() {
        return appConfigName;

    }
}
